package boundarys;

import java.awt.EventQueue;
import java.awt.Image;
import java.awt.PopupMenu;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.Popup;

import controllers.DeleteProductController;
import entitys.Product;
import entitys.Reply;
import enums.Command;
import enums.Result;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.List;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.image.BufferedImage;

public class DeleteProductGUI extends AbstractGUI{

	/**
	 * Delete Product Frame
	 */
	private JFrame deleteProductFrame;
	/**
	 * List of products
	 */
	private List productsList;
	/**
	 * Back btn
	 */
	private JButton btnBack;
	/**
	 * Products ArrayList
	 */
	ArrayList<Product> arr=null;
	/**
	 * Picture label
	 */
	JLabel lblPiclable=null;

	/**
	 * DeleteProduct constructor
	 */
	public DeleteProductGUI() {
		initialize();
		initGUI(deleteProductFrame);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		deleteProductFrame = new JFrame();
		deleteProductFrame.setResizable(false);

		deleteProductFrame.setTitle("...Define Product>Delete Product");
		deleteProductFrame.setBounds(100, 100, 550, 307);
		deleteProductFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		deleteProductFrame.getContentPane().setLayout(null);
		
		
		
		JButton btnDelete = new JButton("Delete");
		
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(productsList.getSelectedItem()!=null)
				{
					DeleteProductController.deleteProduct((String)productsList.getSelectedItem());//send request to delete selected product
				}
				else
				{
					JOptionPane.showMessageDialog(deleteProductFrame, "Please select a product", "CCRM Delete Product", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnDelete.setBounds(291, 190, 89, 23);
		deleteProductFrame.getContentPane().add(btnDelete);
		
	
		
		DeleteProductController.getProducts();
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				goBack();
			}
		});
		btnBack.setBounds(21, 227, 89, 23);
		deleteProductFrame.getContentPane().add(btnBack);
		
		productsList = new List();

		productsList.setBounds(211, 28, 234, 156);
		productsList.addItem("Please Refresh Items");
		deleteProductFrame.getContentPane().add(productsList);
		
		JLabel lblSelectProduct = new JLabel("Select product:");
		lblSelectProduct.setBounds(291, 11, 97, 14);
		deleteProductFrame.getContentPane().add(lblSelectProduct);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DeleteProductController.getProducts();
			}
		});
		btnRefresh.setBounds(451, 91, 89, 23);
		deleteProductFrame.getContentPane().add(btnRefresh);
		
		lblPiclable = new JLabel("");
		lblPiclable.setBounds(10, 11, 195, 156);
		
		
		productsList.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				BufferedImage myPicture = null;
				if(productsList.getSelectedItem()==null)
					return;
				try {
					myPicture = ImageIO.read(new File("images\\"+arr.get(productsList.getSelectedIndex()).getPicturen()+".png"));
					
				
			
				Image myPictureScaled=myPicture.getScaledInstance(lblPiclable.getWidth(), lblPiclable.getHeight(), myPicture.SCALE_SMOOTH);
				deleteProductFrame.getContentPane().remove(lblPiclable);
				deleteProductFrame.getContentPane().validate();
				deleteProductFrame.getContentPane().repaint();
				lblPiclable = new JLabel(new ImageIcon(myPictureScaled));
				lblPiclable.setBounds(10, 11, 195, 156);
				deleteProductFrame.getContentPane().add(lblPiclable);
				deleteProductFrame.getContentPane().revalidate();
				deleteProductFrame.getContentPane().repaint();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					deleteProductFrame.getContentPane().remove(lblPiclable);
					deleteProductFrame.getContentPane().validate();
					deleteProductFrame.getContentPane().repaint();
					lblPiclable = new JLabel("Image is not defined.");
					lblPiclable.setBounds(10, 11, 195, 156);
					deleteProductFrame.getContentPane().add(lblPiclable);
					deleteProductFrame.getContentPane().revalidate();
					deleteProductFrame.getContentPane().repaint();
					System.out.println("Picture isn't defined.");
				}
				catch (java.lang.NullPointerException e1)
				{
					System.out.println("picture wasn't found.");
				}
			}
		});
		
		
		deleteProductFrame.getContentPane().add(lblPiclable);
		deleteProductFrame.setVisible(true);
		
		
	}

	@Override
	/**
	 * AbstractGUI overridden method for getting a reply from server 
	 */
	public void getReply(Object r) {
		
		
		if(((Reply)r).command.equals(Command.GET_PRODUCTS)) //if GET_PRODUCTS reply is received
		{
			arr =(ArrayList<Product>) ((Reply)r).result;
			productsList.removeAll();
			
			for(int i=0;i<arr.size();i++)

				productsList.add(arr.get(i).toString());
			deleteProductFrame.getContentPane().revalidate();
			deleteProductFrame.getContentPane().repaint();
			
		}
		else if(((Reply)r).command.equals(Command.DELETE_PRODUCT))//if DELETE_PRODUCT reply is received
		{
			if (((Reply)r).result.equals(Result.OK))
			{
				System.out.println("Client updated successfully!");
				JOptionPane.showMessageDialog(deleteProductFrame, "Product deleted successfully", "CCRM Update Client", JOptionPane.INFORMATION_MESSAGE);
			}
			System.out.println(((Reply)r).result);
			
			DeleteProductController.getProducts();
			
		}

	}
}
